package com.dodoke.controller;

import java.util.List;
import java.util.Map;

import com.dodoke.common.utils.uuid.IdUtils;
import com.dodoke.domain.WxOrder;
import com.dodoke.domain.vo.WxOrderVo;
import com.dodoke.service.IWxOrderService;
import com.dodoke.validateGroup.OnAdd;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.dodoke.common.annotation.Log;
import com.dodoke.common.core.controller.BaseController;
import com.dodoke.common.core.domain.AjaxResult;
import com.dodoke.common.enums.BusinessType;
import com.dodoke.common.utils.poi.ExcelUtil;
import com.dodoke.common.core.page.TableDataInfo;

import javax.validation.Valid;

/**
 * 【订单】Controller
 *
 * @author dodoke
 * @date 2020-08-25
 */
@RestController
@RequestMapping("/order")
@Api("订单管理")
@Validated
public class WxOrderController extends BaseController
{
    @Autowired
    private IWxOrderService wxOrderService;

    /**
     * 查询【订单】列表
     */
    @RequestMapping("/list/{openId}")
    @ApiOperation("获取用户的订单列表")
    @ApiImplicitParam(name = "openId",value = "用户身份唯一标识",required = true,paramType = "path")
    public TableDataInfo list(@PathVariable String openId)
    {
        startPage();
        List<WxOrder> wxOrdersList = wxOrderService.selectWxOrderList(openId);
        return getDataTable(wxOrdersList);
    }

    /**
     * 查询【订单】列表,给后台使用
     */
    @ApiOperation("查询订单【给Vue后台使用】")
    @ApiImplicitParam(name = "wxOrder",value = "订单对应的实体类",dataType = "WxOrder",paramType = "query")
    @RequestMapping(value = "/selectWxOrderList4Back",method = RequestMethod.GET)
    public TableDataInfo selectWxOrderList4Back(WxOrder wxOrder)
    {
        startPage();
        List<WxOrderVo> list = wxOrderService.selectWxOrderList4Back(wxOrder);
        return getDataTable(list);
    }

    /**
     * 生成【订单】
     */
    @RequestMapping("/addOrder")
    @ApiOperation("生成订单")
    @ApiImplicitParam(name = "wxOrder",value = "订单对应的实体类",dataType = "WxOrder",paramType = "query")
    @Validated({OnAdd.class})
    public AjaxResult addOrder(@Valid @RequestBody WxOrder wxOrder)
    {
        // 订单号
        String number = IdUtils.simpleUUID();
        // 模拟微信产生的订单流水号
        String pay_number = IdUtils.randomUUID();
        wxOrder.setNumber(number);
        wxOrder.setPayNumber(pay_number);
        return toAjax(wxOrderService.insertWxOrder(wxOrder));
    }

    /***
     * 查询订单统计
     * @return
     */
    @ApiOperation("查询订单统计")
    @RequestMapping(value = "/findOrderStat",method = RequestMethod.GET)
    public AjaxResult findOrderStat() {
        return AjaxResult.success(wxOrderService.findOrderStat());
    }

    /***
     * 查询折线图所需的数据
     * @return
     */
    @ApiOperation("查询折线图所需的数据")
    @RequestMapping(value = "/findChartData/{currentMonth}",method = RequestMethod.GET)
    public AjaxResult findChartData(@PathVariable("currentMonth") String currentMonth){
        List<Map<String,Object>> chartData = wxOrderService.findChartData(currentMonth);
        return AjaxResult.success(chartData);
    }

}
